package org.jinghouyu.caculator.engine.expression;

import java.util.List;

import org.jinghouyu.caculator.engine.Expression;

/**
 * 加法
 * @author liujingyu
 *
 */
public class PlusExpression extends Expression {

	@Override
	protected Object _caculate(Object... values) {
		double sum = 0;
		for(Object value : values) {
			sum += getDouble(value);
		}
		return sum;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		List<Expression> es = getSubExpressions();
		for(int i = 0; i < es.size(); i++) {
			sb.append("" + es.get(i));
			if(i != es.size() - 1) {
				sb.append("+");
			}
		}
		return "(" + sb.toString() + ")";
	}
	
	
	
	public static class OneMunisExpression extends Expression {

		@Override
		protected Object _caculate(Object... values) {
			return 0 - getDouble(values[0]);
		}
		
		public String toString() {
			return "0-" + getSubExpression(0);
		}
	}
}
